package com.tax.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tax.config.exception.ApiException;
import com.tax.config.response.Response;
import com.tax.config.response.ResponseCode;
import com.tax.dto.req.SmsCodeReq;
import com.tax.dto.res.RegCodeCache;
import com.tax.entity.TdUser;
import com.tax.enums.RedisKey;
import com.tax.manager.RedisApiManager;
import com.tax.mapper.TdUserMapper;
import com.tax.service.ISmsService;
import com.tax.service.ITdUserService;
import com.tax.service.TdGovCompanyBasicService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

/**
 * FileName: SmsServiceImpl
 * Author:   duandehui
 * Date:     2023/3/4 18:10
 * Description: 短信服务类
 */
@Service
@Slf4j
public class SmsServiceImpl implements ISmsService {

    @Autowired
    private RedisApiManager redisApiManager;

    @Autowired
    private ITdUserService userService;

    @Override
    public Response verificationCodeSend(SmsCodeReq smsCodeParam) {
        String phoneNum = smsCodeParam.getTelephone();
//        String regCode = RandomStringUtils.randomNumeric(6);
        String regCode = "123456";
        String regCodeKey = RedisKey.PHONE_SMS_REG_CODE.getValue(phoneNum);
        // 验证码放入缓存并设置过期时间为15*60秒
        RegCodeCache newRegCodeCache = new RegCodeCache(phoneNum, regCode, Calendar.getInstance().getTimeInMillis());
        boolean updateFlag = redisApiManager.set(regCodeKey, JSONObject.toJSON(newRegCodeCache), 15 * 60, TimeUnit.SECONDS);
        log.info("getSmsCode,updateFlag:{}",updateFlag);

        TdUser tdUser = userService.selectUserByPhone(phoneNum);
        if (null == tdUser) {
            userService.createUser(phoneNum);
        }
        return new Response();
    }

    @Override
    public boolean verifyRegCode(String regCode, String phoneNum) throws ApiException  {
        String regCodeKey = RedisKey.PHONE_SMS_REG_CODE.getValue(phoneNum);
        String regCodeCacheJson = redisApiManager.get(regCodeKey);
        if (StringUtils.isBlank(regCodeCacheJson)) {
            // 验证码失效
            throw new ApiException(ResponseCode.VIERFY_CODE_EXPIRE.getCode()+"",ResponseCode.VIERFY_CODE_EXPIRE.getMsg());
        }


        RegCodeCache regCodeCache = JSON.parseObject(regCodeCacheJson, RegCodeCache.class);
        if (!regCode.equals(regCodeCache.getRegCode())) {
            // 验证码不正确
            throw new ApiException(ResponseCode.REG_CODE_ERROR.getCode()+"",ResponseCode.REG_CODE_ERROR.getMsg());
        }
        return true;
    }

    @Override
    public void removeRegCode(String phoneNum) {
        String regCodeKey = RedisKey.PHONE_SMS_REG_CODE.getValue(phoneNum);
        redisApiManager.expire(regCodeKey,1,TimeUnit.MILLISECONDS);
    }
}
